Python Milestone Project #1 - Tic Tac Toe

  • [x] add functionality to determine if the game is a tie
  • [x] (this is not needed. functions have been added to force only valid inputs) add functionality to format and standardize user input
  • [x] add functionality to determine if the user input is valid
  • [x] add functionality to quit the game at any time
  • [x] add functionality to determine if a cell is already being used
  • [x] reduce code lines by combining x and o functions
  • [x] create a function that prints the board, this should include a grid function that allows selecting a location
    • [x] this needs to handle when x's and o's are placed in board
  • [x] create a function that allows selecting the location
  • [x] create a function that determines when someone has won
  • [x] create a function that gets user input

function that will print the board

the board should look like this:

empty board
   |   |  
-----------
   |   |  
-----------
   |   |  

full board    
 x | o | x
-----------
 x | o | x
-----------
 x | o | x

In [67]:
def print_board(board):
    clear_output()
    horizontal = '----------------'
    print('\n\nTIC TAC TOE\n\n')
    print('   | 1 | 2 | 3 |')
    print(horizontal)
    print(' A |',board['A1'],'|',board['A2'],'|',board['A3'],'|')
    print(horizontal)
    print(' B |',board['B1'],'|',board['B2'],'|',board['B3'],'|')
    print(horizontal)
    print(' C |',board['C1'],'|',board['C2'],'|',board['C3'],'|')
    print(horizontal)
    sleep(.3)
   
    return


def check_for_win(board):
    #check for tie game
    is_tie = 1

    for i in board:
        if board[i] == ' ':
            is_tie = 0

    #horizontal winning combinations
    if board['A1'] == board['A2'] and board['A2'] == board['A3'] and board['A1'] != ' ' and board['A2'] != ' ' and board['A3'] != ' ':
        return 1
    elif board['B1'] == board['B2'] and board['B2'] == board['B3'] and board['B1'] != ' ' and board['B2'] != ' ' and board['B3'] != ' ':
        return 1
    elif board['C1'] == board['C2'] and board['C2'] == board['C3'] and board['C1'] != ' ' and board['C2'] != ' ' and board['C3'] != ' ':
        return 1

    #vertical winning combinations
    elif board['A1'] == board['B1'] and board['B1'] == board['C1'] and board['A1'] != ' ' and board['B1'] != ' ' and board['C1'] != ' ':
        return 1
    elif board['A2'] == board['B2'] and board['B2'] == board['C2'] and board['A2'] != ' ' and board['B2'] != ' ' and board['C2'] != ' ':
        return 1
    elif board['A3'] == board['B3'] and board['B3'] == board['C3'] and board['A3'] != ' ' and board['B3'] != ' ' and board['C3'] != ' ':
        return 1

    #diagnol winning combinations
    elif board['A1'] == board['B2'] and board['B2'] == board['C3'] and board['A1'] != ' ' and board['B2'] != ' ' and board['C3'] != ' ':
        return 1
    elif board['C1'] == board['B2'] and board['B2'] == board['A3'] and board['C1'] != ' ' and board['B2'] != ' ' and board['A3'] != ' ':
        return 1

    #test for a tie
    elif is_tie == 1:
        return 2

    #no winners found
    else:
        return 0


def location_x_o(board, turn_x):
    if turn_x == 1:
        player = 'X'
    else:
        player = 'O'
        
    location = input('\n\nPlayer %s - select a location (i.e. C2 or \"quit\" to exit): ' %(player))
    if location == 'quit':
        return 0

    valid_location = 0
    for i in board:
        if i == location:
            valid_location = 1
    if valid_location == 0:
        print('\nlocation is invalid. please send LETTER and NUMBER using grid system. NO spaces.')
        return location_x_o(board, turn_x)
            
    if board[location] == ' ':
        board[location] = player
    else:
        print('\nlocation already taken. select again.')
        return location_x_o(board, turn_x)
    
    return board
    
       
def quit_playing(turn_x):
    if turn_x == 1:
        player = 'X'
    else:
        player = 'O'
    
    print('\n\nCongratulations player %s! You Won!\n\n' %(player))
    
    i = 1
    while i == 1:
        i = 0
        quit = input('Quit playing? (Y/N): ')
        if quit == 'Y':
            return 1
        elif quit == 'N':
            return 0
        else:
            print('invalid entry. enter only Y or N')
            i = 1


# ---MAIN BODY OF THE GAME---
from IPython.display import clear_output
from time import sleep

# board = {'key1':'value1','key2':'value2'}
board = {'A1':' ','A2':' ','A3':' ','B1':' ','B2':' ','B3':' ','C1':' ','C2':' ','C3':' '}
turn_x = 0
quit = 'N'

while quit == 'N':
    turn_x = not turn_x
    print_board(board)
    board = location_x_o(board, turn_x)

    if board == 0:
        break

    if check_for_win(board) == 1:
        print_board(board)
        if quit_playing(turn_x) == 1:
            break
        else:
            # reinitialize the board
            board = {'A1':' ','A2':' ','A3':' ','B1':' ','B2':' ','B3':' ','C1':' ','C2':' ','C3':' '}
            continue
    elif check_for_win(board) == 2:
        print("\n\nTie game.")
        i = 1
        while i == 1:
            i = 0
            quit = input('Quit playing? (Y/N): ')
            if quit == 'Y':
                break
            elif quit == 'N':
                board = {'A1':' ','A2':' ','A3':' ','B1':' ','B2':' ','B3':' ','C1':' ','C2':' ','C3':' '}
                continue
            else:
                print('invalid entry. enter only Y or N')
                i = 1
    else:
        continue



TIC TAC TOE


   | 1 | 2 | 3 |
----------------
 A | X | O | X |
----------------
 B | X | O |   |
----------------
 C | O | X | O |
----------------


Player X - select a location (i.e. C2 or "quit" to exit): B3


Tie game.
Quit playing? (Y/N): Y

In [ ]: